# encoding: UTF-8:
import binascii

class MCURecvAndDSPSend:
    def __init__(self):
        self.dataFromMCU = ''      ##从mcu 来的data 因为每次发送不能超过128
        self.dataFromDsp = ''      ##从dsp 来的data 要等待拼接
        self.isEnglish = False     ##标志中英文版
        self.isSendMCUData = False ##标志现在是否去mcu
        self.isPrecvStart = False  ##标志上一条是否是开始发送

    def appendCheckSum(self, data):
        sum = 0
        for i in data:
            sum += ord(i)
        data += chr(sum / 16)
        data += chr(sum % 16)
        return data

    def appendOneCheckSum(self,ipset):
        sum = 0
        for char in ipset:
            sum += ord(char)
        sum = sum & 255
        return ipset + chr(sum)

    def requestCommunicate(self):
        if self.isEnglish:
            res = binascii.a2b_hex('a00881608301')
        else:
            res = binascii.a2b_hex('a00841608301')
        res = self.appendCheckSum(res)
        return res

    def endCommunicate(self):
        if self.isEnglish:
            res = binascii.a2b_hex('a00881608302')
        else:
            res = binascii.a2b_hex('a00841608302')
        res = self.appendCheckSum(res)
        return res
    def startDspMCUSend(self):
        if self.isEnglish:
            res = binascii.a2b_hex('A00882818303')
        else:
            res = binascii.a2b_hex('A00842418303')
        res = self.appendCheckSum(res)
        return res
    def endDspMCUSend(self):
        if self.isEnglish:
            res = binascii.a2b_hex('A00882818304')
        else:
            res = binascii.a2b_hex('A00842418304')
        res = self.appendCheckSum(res)
        return res

    def recvMCU(self,data):
        #00 10 60 80 00 00 09 00 65 00 66 01 02 03 04 SUM
        length = ord(data[1]) - 7
        self.dataFromMCU += data[4:4+length]
    # 0,不处理
    # 1，给 dsp
    # 2, 给 pc
    # 3，给 mcu
    def handleMessage(self,data):
        if data == '':  #处理发数据
            if self.dataFromMCU == '':
                return 0,['']
            return 1,[self.requestCommunicate()]
        if data.find('\x87\x00') == 4:
            return 0,['']
        elif data.find('\x87\x01') == 4:
            if self.dataFromMCU == '':
                return 1,[self.endCommunicate()]
            sendData = self.dataFromMCU[:128]
            self.dataFromMCU = self.dataFromMCU[128:]
            length = 13 + len(sendData)
            if self.isEnglish:
                sendData = '\xa0'+chr(length)+binascii.a2b_hex('8180e4')+ chr(len(sendData))+sendData
            else:
                sendData = '\xa0' + chr(length) + binascii.a2b_hex('4140e4') + chr(len(sendData)) + sendData
            return 1,[sendData]
        elif data == self.startDspMCUSend():
            self.isPrecvStart = True
        elif data.find('\xe4') == 4:
            content = data[5:-2]
            self.dataFromDsp += content
            if '\x45\x50\x53\x53\x4a'.find(self.dataFromDsp[:5]) == 0:
                self.isSendMCUData = True
                return 0,[]
            else:
                self.isSendMCUData = False
                sendData = self.dataFromDsp
                self.dataFromDsp = ''
                length = 13 + len(sendData)
                if self.isEnglish:
                    sendData = '\xa0' + chr(length) + binascii.a2b_hex('8180e4') + chr(len(sendData)) + sendData
                else:
                    sendData = '\xa0' + chr(length) + binascii.a2b_hex('4140e4') + chr(len(sendData)) + sendData
                if self.isPrecvStart:
                    self.isPrecvStart = False
                    return 2,[self.startDspMCUSend(),sendData]
                else:
                    return 2,[sendData]
        elif data == self.endDspMCUSend():
            if self.isSendMCUData:
                length = 7 + len(self.dataFromDsp[6:])
                if self.isEnglish:
                    sendData ='\x00'+chr(length)+ binascii.a2b_hex('80600000')+self.dataFromDsp[6:]
                else:
                    sendData = '\x00' + chr(length) + binascii.a2b_hex('40600000') + self.dataFromDsp[6:]
                sendData = self.appendOneCheckSum(sendData)
                return 3,[sendData]
            else:
                return 2,[data]
        return 0,[]

if __name__ == '__main__':
    sendData = binascii.a2b_hex('00106040000009006500660102030400')
    mcuRecvAndDSPSend = MCURecvAndDSPSend()
    mcuRecvAndDSPSend.recvMCU(sendData)
    print binascii.b2a_hex(mcuRecvAndDSPSend.dataFromMCU)
    code , msgArr =  mcuRecvAndDSPSend.handleMessage(binascii.a2b_hex('a008424187010000'))

    for msg in msgArr:
        print binascii.b2a_hex(msg)









